﻿@model OT.Data.User

<meta name="google-signin-clientid" content="1057923670953-jjanjd9lk88mcpesgjtu9g2fhnl2prb8.apps.googleusercontent.com" />
<meta name="google-signin-scope" content="https://www.googleapis.com/auth/plus.login" />
<meta name="google-signin-requestvisibleactions" content="http://schema.org/AddAction" />
<meta name="google-signin-cookiepolicy" content="single_host_origin" />
<div class="modal-content">
    <div class="modal-header">
        <button class="close" aria-hidden="true" data-dismiss="modal" type="button">×</button>
        <h4>Login</h4>
    </div>
    <div class="modal-body">
        <div id="divMsgIndex"></div>

        <div class="form">
            <form class="form-horizontal">
                <div class="form-group">
                    <label class="control-label col-md-4" for="username"> Username</label>
                    <div class="col-md-6">
                        <input id="EmailAddress" class="form-control" type="text" placeholder="Enter Email ID">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-4" for="email"> Password</label>
                    <div class="col-md-6">
                        <input id="Password" class="form-control" type="password"  placeholder="Enter PassWord">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-4"></div>
                        <div class="col-md-6">
                            <span></span>Login As  
                                <input type="radio" value="3" name="UserTypeID" title="Student" checked/> Student
                                <input type="radio" value="2" name="UserTypeID" title="Instructor"/> Insructor  
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-4"></div>
                    <div class="col-md-6">
                        <button class="btn btn-default" id="btnlogin">Login</button>
                        <button class="btn btn-default" type="reset">Reset</button>
                    </div>
                </div>
                 <div class="form-group">
                    <div class="col-md-4"></div>
                    <div class="col-md-6">
                        <button class="btn btn-danger"  onclick="login()"><i class="fa fa-google"></i>Google+ Login</button>
                     </div>
                </div>
            </form>
        </div>
                                
    </div>
    <div class="modal-footer">
        <div class="pull-left">        <p>Forgot PassWord ?
            <a href="~/Home/ForgotPword">Click Here</a>
        </p>
            </div>
        <div class="pull-right">
        <p>
            Don't have an account?
            <a href="~/Home/CreateStudent">Register</a>
            here.
        </p></div>
    </div>
</div>


    <script type="text/javascript">

        $("#btnforget").click(function (e) {
            e.preventDefault();
            $("#modalContent").load(this.href, function () {
                $('#modalDiv').modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
            });
            return false;
        });
        $("#btnlogin").click(function (e) {
            debugger;
            var EmailID = btoa($("#EmailAddress").val());
            var Password = btoa ($("#Password").val());
            var UserTypeID = $('input[name="UserTypeID"]:checked').val();
            e.preventDefault();
            if (EmailID == '' || Password == '') {
                showMsg('divMsgIndex', 'Must Be Enter EmailID and PassWord', 'danger');
            }
            else {
                $.ajax({
                    url: "../Login/login?EmailID=" + EmailID + "&Password=" + Password + "&UserTypeID=" + UserTypeID,
                    type: "POST",
                    success: function (result) {
                        if (result != "") {
                            if (result == "Instructor") {
                                window.location = "/../instructor";
                            } else if (result == "Student") {
                                window.location = "/../Student";
                            }
                            else {
                                showMsg('divMsgIndex', result, 'danger');
                            }
                        }
                        else {
                            showMsg('divMsgIndex', result, 'danger');
                        }
                    },
                    error: function (data) {
                    }
                });
            }

        });
      
        $(document).ready(function () {
            var isValid = '@Html.Raw(ViewData.ModelState.IsValid)';
            //if (isValid == "True")
            //{ document.getElementById("error").style.display = "none" }
            //else
            //{ document.getElementById("error").style.display = "flex" }
        });

        window.fbAsyncInit = function () {
            FB.init({
                appId: '328072837355998', // App ID
                channelUrl: '//' + window.location.hostname + '/home',
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true,  // parse XFBML
                version: 'v2.0'
            });
        }

        $('#facebook').click(function (e) {
            FB.login(function (response) {
                FB.getLoginStatus(function (response) {
                    if (response.status === 'connected') {
                        FB.api('/me?fields=first_name,last_name,id,email,location', function (response) {
                            $.get("/Home/Avail", { EmailID1: response.email },
                                   function (classesData) {
                                       if (classesData <= 0) {
                                           $.getJSON("/Home/FacebookCallback", { EmailAddress: response.email, FirstName: response.first_name, LastName: response.last_name, Address: response.location.name },
                                             function () {
                                                 window.location = "/Student";
                                             });
                                       }
                                       else {
                                           $.getJSON("/Home/GetUserDetail", { EmailID: response.email },
                                              function (Data) {
                                                  if (Data != null || Data != undefined) {
                                                      $.ajax({
                                                          type: "POST",
                                                          url: "/Home/StudentLogin?FirstName=" + Data.FirstName + "&UserID=" + Data.UserID,
                                                          success: function (data) {
                                                              window.location = "/Student";
                                                          },
                                                          error: function (error, txtStatus) {
                                                              console.log(txtStatus);
                                                              console.log('error');
                                                          }
                                                      });
                                                  }
                                              });
                                       }
                                   });
                        });
                    }
                });

            }, { scope: 'publish_actions,email,user_location' });
        });

        $('#ref').click(function () {
            $.ajax({
                type: "GET",
                url: '@Url.Action("Home")',
                contentType: "image/png",
                success: function (data) {
                },
                error: function (error, txtStatus) {
                    console.log(txtStatus);
                    console.log('error');
                }
            });
        });
    </script>

    <div id="profile"></div>
    <script type="text/javascript">
        $(window).bind("beforeunload", function () {
            gapi.auth.signOut();
        })

        function logout() {
            gapi.auth.signOut();
            location.reload();
        }

        function login() {
            var myParams = {
                'clientid': '1057923670953-jjanjd9lk88mcpesgjtu9g2fhnl2prb8.apps.googleusercontent.com',
                'cookiepolicy': 'single_host_origin',
                'callback': 'loginCallback',
                'approvalprompt': 'force',
                'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read'
            };
            gapi.auth.signIn(myParams);
        }

        function loginCallback(result) {
            if (result['status']['signed_in']) {
                var request = gapi.client.plus.people.get({ 'userId': 'me' });
                request.execute(function (resp) {
                    var email = '';
                    if (resp['emails']) {
                        for (i = 0; i < resp['emails'].length; i++) {
                            if (resp['emails'][i]['type'] == 'account') {
                                email = resp['emails'][i]['value'];
                            }
                        }
                    }
                    var fname = resp['name']['givenName'];
                    var lname = resp['name']['familyName'];
                    $.get("/Home/Avail", { EmailID1: email },
                                      function (classesData) {
                                          if (classesData <= 0) {
                                              $.getJSON("/Home/GoogleCall", { EmailAddress: email, FirstName: fname, LastName: lname },
                                                function (res) {
                                                    window.location = "/Student";
                                                });
                                          }
                                          else {
                                              $.getJSON("/Home/GetUserDetail", { EmailID: email },
                                                 function (Data) {
                                                     if (Data != null || Data != undefined) {
                                                         $.ajax({
                                                             type: "POST",
                                                             url: "/Home/StudentLogin?FirstName=" + Data.FirstName + "&UserID=" + Data.UserID,
                                                             success: function (data) {
                                                                 window.location = "/Student";
                                                             },
                                                             error: function (error, txtStatus) {
                                                                 console.log(txtStatus);
                                                                 console.log('error');
                                                             }
                                                         });
                                                     }
                                                 });
                                          }
                                      });
                    var str = "Name:" + resp['displayName'] + "<br>";
                    str += "Image:" + resp['name']['familyname'] + "<br>";
                    str += "Image:" + resp['name']['givenName'] + "<br>";
                    str += "Image:" + resp['image']['url'] + "<br>";
                    str += "<img src='" + resp['image']['url'] + "' /><br>";
                    str += "URL:" + resp['url'] + "<br>";
                    str += "Email:" + email + "<br>";
                });
            }
        }

        function onLoadCallback() {
            // gapi.client.setApiKey('');
            gapi.client.load('plus', 'v1', function () { });
        }
    </script>

    <script type="text/javascript">
        (function () {
            var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
            po.src = 'https://apis.google.com/js/client.js?onload=onLoadCallback';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
        })();
    </script>
